package com.siqi.hr.system.custom.entity;

import com.siqi.hr.system.organization.entity.Role;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 角色实体操作对象
 * @author    xiaoxm
 * Created by xiaogoingqin on 2016/7/24.
 */
public class EntityOperation {

    public EntityOperation() {
    }

    public EntityOperation(String id,String name) {
        this.id = id;
        this.name = name;
    }

    public EntityOperation(String name,String method,String url,String icon,String showHere,EntityRecord entityRecord) {
        this.name = name;
        this.method = method;
        this.url = url;
        this.icon = icon;
        this.showHere = showHere;
        this.entityRecord = entityRecord;
    }

    /**
     * 操作id
     */
    private String id;

    /**
     * 操作名称
     */
    private String name;

    /**
     * 所属的实体
     */
    private EntityRecord entityRecord;

    /**
     * 所属角色
     */
    private Role role;

    /**
     * 操作类型(0:添加table标签，1：弹窗，2：url跳转)
     */
    private int operationType;

    /**
     *  是否是系统操作0:否，1：是 默认取值为0
     */
    private Boolean sysOperation;

    /**
     *  操作按钮显示在哪里 showList：显示在列表上，showColumn:显示在列前，showView：显示在查看页面，其他地方可以添加
     */
    private String showHere;

    /**
     * 按钮是否显示在列表上
     */
    private Boolean showList;

    /**
     * 按钮是否显示在列前
     */
    private Boolean showColumn;

    /**
     * 按钮是否显示在查看页
     */
    private Boolean showView;

    /**
     * 是否受权限控制操作(true:是,false:否,默认值为false)
     */
    private Boolean permission;

    /**
     * 触发的方法名
     */
    private String method;

    /**
     * url地址
     */
    private String url;

    /**
     * 排序
     */
    private int position;

    /**
     * 存放操作图标
     */
    private String  icon ;

    /**
     * 描述/备注
     */
    private String description;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public EntityRecord getEntityRecord() {
        return entityRecord;
    }

    public void setEntityRecord(EntityRecord entityRecord) {
        this.entityRecord = entityRecord;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getOperationType() {
        return operationType;
    }

    public void setOperationType(int operationType) {
        this.operationType = operationType;
    }

    public Boolean isSysOperation() {
        return sysOperation;
    }

    public void setSysOperation(Boolean sysOperation) {
        this.sysOperation = sysOperation;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof EntityOperation) {
            return this.name.equals(((EntityOperation)obj).getName()) && method.equals(((EntityOperation)obj).getMethod())
               && this.icon.equals(((EntityOperation)obj).getIcon());
        }
        return super.equals(obj);
    }
    @Override
    public int hashCode() {
        if(name == null)
            return super.hashCode();
        return name.hashCode();
    }

    public Boolean getShowList() {
        return showList;
    }

    public void setShowList(Boolean showList) {
        this.showList = showList;
    }

    public Boolean getShowColumn() {
        return showColumn;
    }

    public void setShowColumn(Boolean showColumn) {
        this.showColumn = showColumn;
    }

    public Boolean getShowView() {
        return showView;
    }

    public void setShowView(Boolean showView) {
        this.showView = showView;
    }

    public String getShowHere() {
        return showHere;
    }

    public void setShowHere(String showHere) {
        this.showHere = showHere;
    }

    public Boolean getPermission() {
        return permission;
    }

    public void setPermission(Boolean permission) {
        this.permission = permission;
    }

    /**
     * 查询所有操作
     * @date 07 24, 2016
     */
    public static class getMainOperation extends EntityOperation implements RowMapper<EntityOperation> {
        public EntityOperation mapRow(ResultSet rs, int rowNum) throws SQLException {
            EntityOperation opt = new EntityOperation();
            opt.setId(rs.getString("id"));
            opt.setName(rs.getString("name"));
            return opt;
        }
    }

    /**
     * 查询所有操作
     * @date 07 24, 2016
     */
    public static class getAllOperation extends EntityOperation implements RowMapper<EntityOperation> {
        public EntityOperation mapRow(ResultSet rs, int rowNum) throws SQLException {
            EntityOperation opt = new EntityOperation();
            opt.setId(rs.getString("id"));
            opt.setName(rs.getString("name"));
            opt.setOperationType(rs.getInt("operationType"));
            opt.setSysOperation(rs.getBoolean("sysOperation"));
            //opt.setIsHere(rs.getString("isHere"));
            opt.setShowList(rs.getBoolean("showList"));
            opt.setShowColumn(rs.getBoolean("showColumn"));
            opt.setShowView(rs.getBoolean("showView"));
            opt.setPosition(rs.getInt("position"));
            opt.setIcon(rs.getString("icon"));
            opt.setDescription(rs.getString("description"));
            return opt;
        }
    }

    /**
     * 查询角色对应实体所以操作
     * @date 07 24, 2016
     */
    public static class getEntityRoleOperation extends EntityOperation implements RowMapper<EntityOperation> {
        public EntityOperation mapRow(ResultSet rs, int rowNum) throws SQLException {
            EntityOperation opt = new EntityOperation();
            opt.setId(rs.getString("id"));
            opt.setName(rs.getString("name"));
            opt.setRole(new Role(rs.getString("role_id")));
            opt.setEntityRecord(new EntityRecord(rs.getInt("entity_id")));
            opt.setOperationType(rs.getInt("operationType"));
            opt.setSysOperation(rs.getBoolean("sysOperation"));
            opt.setShowHere(rs.getString("showHere"));
            opt.setMethod(rs.getString("method"));
            opt.setIcon(rs.getString("icon"));
            opt.setDescription(rs.getString("description"));
            return opt;
        }
    }
}
